﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.Manager
{
    public partial class StockEditor : UserControl
    {
        Stock m_currentStock;

        public StockEditor()
        {
            InitializeComponent();

            this.cbKind.Items.Add("金融");
            this.cbKind.Items.Add("非金融");
            this.cbKind.SelectedIndex = 0;

            this.cbLocation.Items.Add("上海");
            this.cbLocation.Items.Add("深圳");
            this.cbLocation.SelectedIndex = 0;

            foreach (string trade in JYStockAnalyser.ClientLib.ClientCache.Instance.AllTrades)
            {
                this.cbTrade.Items.Add(trade);
            }            
            this.cbTrade.SelectedIndex = 0;
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        public Stock GetStock()
        {
            if (m_currentStock == null)
                m_currentStock = new Stock();

            m_currentStock.NO = this.txtNO.Text.Trim();
            m_currentStock.Name = this.txtName.Text.Trim();
            m_currentStock.Kind = this.cbKind.SelectedIndex + 1;
            m_currentStock.CompanyName = this.txtCompanyName.Text.Trim();
            m_currentStock.Province = this.txtProvince.Text.Trim();
            m_currentStock.Trade = (string)this.cbTrade.SelectedItem;
            m_currentStock.address = this.txtAddress.Text.Trim();
            m_currentStock.Location = (string)this.cbLocation.SelectedItem;
            
            return m_currentStock;
        }

        public void SetStock(Stock stock)
        {
            m_currentStock = stock;

            this.txtNO.Text = stock.NO;
            this.txtName.Text = stock.Name;
            this.cbKind.SelectedIndex = stock.Kind - 1;
            this.txtCompanyName.Text = stock.CompanyName;
            this.txtProvince.Text = stock.Province;
            this.cbTrade.SelectedItem = stock.Trade;
            this.txtAddress.Text = stock.address;
            this.cbLocation.SelectedItem = stock.Location;
        }

        public bool Vali()
        {
            if (String.IsNullOrEmpty(this.txtNO.Text.Trim()))
            {
                Tool.ShowInformationBox("必须输入编号");
                this.txtNO.Focus();
                return false;
            }

            if (String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                Tool.ShowInformationBox("必须输入名称");
                this.txtName.Focus();
                return false;
            }

            return true;
        }
    }
}
